package com.way.capture.adapter;

import android.graphics.drawable.Drawable;
import androidx.core.content.ContextCompat;
import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.way.capture.R;
import com.way.capture.data.DataInfo;
import com.way.capture.utils.Utils;
import com.way.capture.utils.glide.GlideHelper;

import java.util.HashSet;
import java.util.List;

/**
 * Created by android on 16-2-1.
 */
public class ScreenshotAdapter extends BaseQuickAdapter<DataInfo, BaseViewHolder> {
    private HashSet<Integer> mSelected;

    public ScreenshotAdapter(int layoutResId) {
        super(layoutResId);
        setHasStableIds(true);
        mSelected = new HashSet<>();
    }

    public void removeMulData(List<DataInfo> dataInfos) {
        mData.removeAll(dataInfos);
        notifyDataSetChanged();
    }

    public void clearData() {
        mData.clear();
        notifyDataSetChanged();
    }

    @Override
    public DataInfo getItem(int pos) {
        return mData.get(pos);
    }

    @Override
    protected void convert(BaseViewHolder helper, DataInfo item) {
        helper.itemView.setTag(item.path);
        ImageView imageView = helper.getView(R.id.iv_image);

        ImageView selectImageView = helper.getView(R.id.cb_selected);
        View coverView = helper.getView(R.id.lay_mask);
        GlideHelper.loadResourceBitmap(item.path, imageView);

        boolean isSelected = mSelected.contains(helper.getAdapterPosition());
        imageView.animate().scaleX(isSelected ? 0.8f : 1.0f).scaleY(isSelected ? 0.8f : 1.0f);
        selectImageView.setVisibility(isSelected ? View.VISIBLE : View.GONE);
        coverView.setVisibility(isSelected ? View.VISIBLE : View.GONE);

        if (item.type == DataInfo.TYPE_SCREEN_RECORD) {
            final Drawable indicatorBackground
                    = ContextCompat.getDrawable(imageView.getContext(), R.drawable.ic_play_bg);
            final Drawable indicatorOverlay
                    = ContextCompat.getDrawable(imageView.getContext(), R.drawable.ic_play);
            imageView.post(() -> {
                final int overlayDimens = (int) (imageView.getWidth() * 0.3f);
                indicatorBackground.setBounds(
                        imageView.getWidth() / 2 - overlayDimens / 2,
                        imageView.getHeight() / 2 - overlayDimens / 2,
                        imageView.getWidth() / 2 + overlayDimens / 2,
                        imageView.getHeight() / 2 + overlayDimens / 2);
                indicatorOverlay.setBounds(
                        imageView.getWidth() / 2 - overlayDimens / 2 + Utils.dp2px(4),
                        imageView.getHeight() / 2 - overlayDimens / 2 + Utils.dp2px(4),
                        imageView.getWidth() / 2 + overlayDimens / 2 - Utils.dp2px(4),
                        imageView.getHeight() / 2 + overlayDimens / 2 - Utils.dp2px(4));
                imageView.getOverlay().add(indicatorBackground);
                imageView.getOverlay().add(indicatorOverlay);
            });
        } else {
            imageView.getOverlay().clear();
        }
    }

    // ----------------------
    // Selection
    // ----------------------
    public void toggleSelection(int pos) {
        if (mSelected.contains(pos))
            mSelected.remove(pos);
        else
            mSelected.add(pos);
        notifyItemChanged(pos);
    }

    public void select(int pos, boolean selected) {
        if (selected)
            mSelected.add(pos);
        else
            mSelected.remove(pos);
        notifyItemChanged(pos);
    }

    public void selectRange(int start, int end, boolean selected) {
        for (int i = start; i <= end; i++) {
            if (selected)
                mSelected.add(i);
            else
                mSelected.remove(i);
        }
        notifyItemRangeChanged(start, end - start + 1);
    }

    public void deselectAll() {
        // this is not beautiful...
        mSelected.clear();
        notifyDataSetChanged();
    }

    public void selectAll() {
        for (int i = 0; i < getItemCount(); i++)
            mSelected.add(i);
        notifyDataSetChanged();
    }

    public int getCountSelected() {
        return mSelected.size();
    }

    public HashSet<Integer> getSelection() {
        return mSelected;
    }
}
